from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from booktest.models import bookinfo


class TestView(View):
    def get(self, request):
        dict1 = {
            'name':'haha',
            'age':18
        }
        return render(request, 'booktest/haha.html', dict1)


class BookView(View):
    def get(self, request):
        books = bookinfo.objects.all()
        dict2 = {
            'books':books
        }

        # 设置cookie
        res = render(request, 'booktest/books.html', dict2)
        res.set_cookie("name", 'yycr', max_age=100000)

        # 存session
        request.session['name'] = '小明'
        request.session['age'] = 20
        request.session['gender'] = 0

        # 返回
        return res


class CookieView(View):
    """
    提取并打印设置的cookie
    """
    def get(self, request):
        name = request.COOKIES.get('name')
        print(name)
        n1 = request.session.get('name')
        age1 = request.session.get('age')
        gender1 = request.session.get('gender')
        print(n1)
        print(age1)
        print(gender1)
        return HttpResponse(name)


